package com.iwhalecloud.bss.kite.cucc.service.context;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import com.iwhalecloud.bss.kite.client.dto.cust.CustDTO;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegion;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.manager.dto.InstIdMapping;
import com.iwhalecloud.bss.kite.service.context.ServiceContextData;
import com.ztesoft.bss.rul.core.client.dto.instspec.InstSpecRel;

public class CuccServiceContext {

    private CuccServiceContext() {

    }

    /**
     * 上下文对象
     */
    private static final ThreadLocal<ServiceContextData> THREADLOCALDATA = new ThreadLocal<ServiceContextData>();

    /**
     * 缓存当前线程查询过的客户信息
     */
    private static final ThreadLocal<Map<String, CustDTO>> CUSTMAPLOCALDATA = new ThreadLocal<>();

    /**
     * 缓存当前线程查询过的客户信息
     */
    private static final ThreadLocal<Map<String, List<UnicomRegion>>> REGIONMAPLOCALDATA = new ThreadLocal<>();

    /**
     * 缓存当前线程查询过的客户信息
     */
    private static final ThreadLocal<Map<String, List<List<UnicomRegion>>>> INNERREGIONMAPLOCALDATA = new ThreadLocal<>();

    /**
     * 缓存当前线程接口初始化instId参数
     */
    private static final ThreadLocal<Map<String, LinkedList<InstIdMapping>>>  INTERFACEINSTIDMAP = new ThreadLocal<>();

    /**
     * 上下文对象 (附加规则对象)
     */
    private static final ThreadLocal<Map<String, List<InstSpecRel>>> EXTINSTSPECRELLOCALDATA = new ThreadLocal<Map<String, List<InstSpecRel>>>();

    private static final String PRIV_DESENSITIZATION = "IS_DESENSITIZATION"; //查看敏感脱敏信息权限

    /**
     * 判断信息是否需要脱敏
     */
    private static final ThreadLocal<Map<String, Boolean>> DESENSITIZATIONDATA = new ThreadLocal<>();

    /**
     * 上下文对象 (规则失效标识)
     */
    private static final ThreadLocal<Map<String, String>> RULEDISABLEDATA = new ThreadLocal<Map<String, String>>();

    /**
     * 上下文对象 (扩展参数)
     */
    private static final ThreadLocal<Map<String, Object>> EXTPARAMSLOCALDATA = new ThreadLocal<Map<String, Object>>();

    /**
     *
     */
    public static Map<String, LinkedList<InstIdMapping>> getInterfaceInstIdMap() {
        return INTERFACEINSTIDMAP.get();
    }

    public static void setInterfaceInstIdMap(Map<String, LinkedList<InstIdMapping>> allInitInstIdMaps) {
        INTERFACEINSTIDMAP.set(allInitInstIdMaps);
    }

    /**
     * 获取是否调外部接口初始化属性值
     * 1、业务变更还原属性值时不需要调接口初始化属性值
     * @return
     */
    public static Boolean getIsCallInfForInitAttr() {
        Map<String, Object> extParams = EXTPARAMSLOCALDATA.get();
        if (KiteMapUtils.isNotEmpty(extParams)) {
            Boolean isExecuteAfter = KiteMapUtils.getBoolean(extParams, LocalKeyConsts.IS_CALL_INF_FOR_INIT_ATTR);
            return Optional.ofNullable(isExecuteAfter).orElse(Boolean.TRUE);
        }
        else {
            return Boolean.TRUE;
        }
    }

    /**
     * 设置是否调外部接口初始化属性值
     * 1、业务变更还原属性值时不需要调接口初始化属性值
     * @param isCallInfForInitAttr
     */
    public static void setIsCallInfForInitAttr(Boolean isCallInfForInitAttr) {
        Map<String, Object> extParams = EXTPARAMSLOCALDATA.get();
        if (Objects.isNull(extParams)) {
            extParams = new HashMap<>();
        }
        extParams.put(LocalKeyConsts.IS_CALL_INF_FOR_INIT_ATTR, isCallInfForInitAttr);
        EXTPARAMSLOCALDATA.set(extParams);
    }

    public static List<UnicomRegion> getRegions(String key) {
        Map<String, List<UnicomRegion>> regionMap = REGIONMAPLOCALDATA.get();
        if (KiteMapUtils.isNotEmpty(regionMap)) {
            List<UnicomRegion> regions = (List<UnicomRegion>) KiteMapUtils.getObject(regionMap, key);
            return regions;
        }
        return null;
    }

    public static void setRegions(String key, List<UnicomRegion> regions) {
        Map<String, List<UnicomRegion>> regionMap = REGIONMAPLOCALDATA.get();
        if (Objects.isNull(regionMap)) {
            regionMap = new HashMap<>();
        }
        regionMap.put(key, regions);
        REGIONMAPLOCALDATA.set(regionMap);
    }

    public static List<List<UnicomRegion>> getInnerRegions(String key) {
        Map<String, List<UnicomRegion>> regionMap = REGIONMAPLOCALDATA.get();
        if (KiteMapUtils.isNotEmpty(regionMap)) {
            List<List<UnicomRegion>> regions = (List<List<UnicomRegion>>) KiteMapUtils.getObject(regionMap, key);
            return regions;
        }
        return null;
    }

    public static void setInnerRegions(String key, List<List<UnicomRegion>> regions) {
        Map<String, List<List<UnicomRegion>>> regionMap = INNERREGIONMAPLOCALDATA.get();
        if (Objects.isNull(regionMap)) {
            regionMap = new HashMap<>();
        }
        regionMap.put(key, regions);
        INNERREGIONMAPLOCALDATA.set(regionMap);
    }
}
